package com.lineage.server.clientpackets;

import com.add.L1PcUnlock;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.MapsTable;
import com.lineage.server.datatables.lock.ClanAllianceReading;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Alliance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1War;
import com.lineage.server.serverpackets.S_MapTimerOut;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.FaceToFace;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldWar;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Rank extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Rank.class);
  
  private static final Random _random = new Random();
  
  private int _giverank;
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      L1PcInstance targetPc;
      L1Clan clan1, clan2, clan3;
      Random random;
      int g_entertime;
      L1Clan clan;
      L1Alliance alliance1;
      L1PcInstance alliancePc;
      L1Alliance alliance3;
      long time;
      int g_maxTime, userRank, g_newTime;
      List<Integer> rankList;
      Calendar cal;
      S_ServerMessage rocksPrison;
      long h_time;
      int i_entertime, i_maxTime, n, i_newTime, addhp;
      S_ServerMessage ivorytower;
      int l_entertime, l_maxTime, l_newTime;
      S_ServerMessage lastabard;
      int d_entertime, d_maxTime, d_newTime;
      S_ServerMessage dragonvalley;
      read(decrypt);
      int data = 0;
      int rank = 0;
      String name = "";
      try {
        data = readC();
        rank = readC();
        name = readS();
      } catch (Exception e3) {
        return;
      } 
      L1PcInstance pc = client.getActiveChar();
      if (pc == null)
        return; 
      switch (data) {
        case 1:
          targetPc = World.get().getPlayer(name);
          if (targetPc == null) {
            targetPc = CharacterTable.get().restoreCharacter(name);
            if (targetPc == null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(109, name));
              return;
            } 
          } 
          clan = WorldClan.get().getClan(pc.getClanname());
          if (clan == null)
            return; 
          userRank = pc.getClanRank();
          if (userRank == 4) {
            switch (rank) {
              case 7:
                this._giverank = 2;
                break;
              case 8:
                this._giverank = 5;
                break;
              case 9:
                this._giverank = 6;
                break;
            } 
          } else {
            this._giverank = rank;
          } 
          if (targetPc.getClanRank() == 4 || targetPc.getClanRank() == 10) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
            return;
          } 
          if (targetPc.getClanRank() == 3) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(2064));
            return;
          } 
          if (targetPc.getClanRank() == pc.getClanRank()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
            return;
          } 
          if (userRank != 3 && userRank != 4 && userRank != 6 && userRank != 9 && userRank != 10) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("守护骑士以上阶级才能使用此命令。"));
            return;
          } 
          if (name.equalsIgnoreCase(pc.getName())) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(2068));
            return;
          } 
          if (this._giverank == 0 || this._giverank > 10) {
            System.out.println("阶级错误");
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(2149));
            return;
          } 
          rankList = new ArrayList<>();
          switch (userRank) {
            case 3:
              if (pc.getLevel() < 25) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2471));
                return;
              } 
              rankList.add(Integer.valueOf(2));
              rankList.add(Integer.valueOf(5));
              rankList.add(Integer.valueOf(6));
              break;
            case 4:
              if (pc.getLevel() < 25) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2471));
                return;
              } 
              rankList.add(Integer.valueOf(2));
              rankList.add(Integer.valueOf(3));
              rankList.add(Integer.valueOf(5));
              rankList.add(Integer.valueOf(6));
              break;
            case 6:
              if (pc.getLevel() < 40) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2472));
                return;
              } 
              rankList.add(Integer.valueOf(2));
              rankList.add(Integer.valueOf(5));
              break;
            case 9:
              if (pc.getLevel() < 40) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2472));
                return;
              } 
              rankList.add(Integer.valueOf(7));
              rankList.add(Integer.valueOf(8));
              break;
            case 10:
              if (pc.getLevel() < 25) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2471));
                return;
              } 
              rankList.add(Integer.valueOf(7));
              rankList.add(Integer.valueOf(8));
              rankList.add(Integer.valueOf(9));
              break;
          } 
          switch (this._giverank) {
            case 2:
              if (!rankList.contains(Integer.valueOf(2))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
            case 5:
              if (!rankList.contains(Integer.valueOf(5))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
            case 6:
              if (targetPc.getLevel() < 40) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2473));
                return;
              } 
              if (!rankList.contains(Integer.valueOf(6))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
            case 7:
              if (!rankList.contains(Integer.valueOf(7))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
            case 8:
              if (!rankList.contains(Integer.valueOf(8))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
            case 9:
              if (targetPc.getLevel() < 40) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2473));
                return;
              } 
              if (!rankList.contains(Integer.valueOf(9))) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(2065));
                return;
              } 
              break;
          } 
          if (pc.getClanid() == targetPc.getClanid()) {
            try {
              targetPc.setClanRank(this._giverank);
              targetPc.save();
              pc.sendPackets((ServerBasePacket)new S_PacketBox(27, this._giverank, name));
              if (targetPc.getOnlineStatus() == 1) {
                L1PcUnlock.Pc_Unlock(targetPc);
                targetPc.sendPackets((ServerBasePacket)new S_PacketBox(27, this._giverank, name));
              } 
            } catch (Exception e) {
              _log.error(e.getLocalizedMessage(), e);
            } 
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(201, name));
          return;
        case 2:
          clan1 = pc.getClan();
          if (clan1 == null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1064));
            return;
          } 
          alliance1 = ClanAllianceReading.get().getAlliance(clan1.getClanId());
          if (alliance1 != null) {
            StringBuffer sbr = new StringBuffer();
            for (L1Clan l1clan : alliance1.getTotalList()) {
              if (clan1.getClanId() != l1clan.getClanId())
                sbr.append(l1clan.getClanName()).append(" "); 
            } 
            pc.sendPackets((ServerBasePacket)new S_PacketBox(62, sbr.toString()));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1233));
          break;
        case 3:
          clan2 = pc.getClan();
          if (clan2 == null || pc.getId() != clan2.getLeaderId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(518));
            return;
          } 
          if (pc.getLevel() < 25 || !pc.isCrown()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1206));
            return;
          } 
          if (ClanAllianceReading.get().getAlliance(clan2.getClanId()) != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1202));
            return;
          } 
          for (L1War war : WorldWar.get().getWarList()) {
            if (war.checkClanInWar(clan2.getClanName())) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1234));
              return;
            } 
          } 
          alliancePc = FaceToFace.faceToFace(pc);
          if (alliancePc != null) {
            if (!alliancePc.isCrown()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(92, alliancePc.getName()));
              return;
            } 
            if (alliancePc.getClanid() == 0 || alliancePc.getClan() == null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(90, alliancePc.getName()));
              return;
            } 
            alliancePc.setTempID(pc.getId());
            alliancePc.sendPackets((ServerBasePacket)new S_Message_YN(223, pc.getName()));
          } 
          break;
        case 4:
          clan3 = pc.getClan();
          if (clan3 == null || pc.getId() != clan3.getLeaderId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(518));
            return;
          } 
          for (L1War war : WorldWar.get().getWarList()) {
            if (war.checkClanInWar(clan3.getClanName())) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1203));
              return;
            } 
          } 
          alliance3 = ClanAllianceReading.get().getAlliance(clan3.getClanId());
          if (alliance3 != null) {
            pc.sendPackets((ServerBasePacket)new S_Message_YN(1210));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1233));
          break;
        case 5:
          if (pc.get_food() < 225)
            break; 
          if (pc.getWeapon() == null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1973));
            break;
          } 
          random = new Random();
          time = pc.get_h_time();
          cal = Calendar.getInstance();
          h_time = cal.getTimeInMillis() / 1000L;
          n = (int)((h_time - time) / 60L);
          addhp = 0;
          if (n <= 0) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1974));
          } else if (n >= 1 && n <= 29) {
            addhp = (int)(pc.getMaxHp() * n / 100.0D);
          } else if (n >= 30) {
            int lv = pc.getWeapon().getEnchantLevel();
            switch (lv) {
              case 0:
              case 1:
              case 2:
              case 3:
              case 4:
              case 5:
              case 6:
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8907));
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8684));
                addhp = (int)(pc.getMaxHp() * (random.nextInt(20) + 20) / 100.0D);
                break;
              case 7:
              case 8:
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8909));
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8685));
                addhp = (int)(pc.getMaxHp() * (random.nextInt(20) + 30) / 100.0D);
                break;
              case 9:
              case 10:
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8910));
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8773));
                addhp = (int)(pc.getMaxHp() * (random.nextInt(10) + 50) / 100.0D);
                break;
              default:
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8908));
                pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), 8686));
                addhp = (int)(pc.getMaxHp() * 0.7D);
                break;
            } 
          } 
          if (addhp != 0) {
            pc.set_food(0);
            pc.sendPackets((ServerBasePacket)new S_PacketBox(11, 0));
            pc.setCurrentHp(pc.getCurrentHp() + addhp);
          } 
          break;
        case 6:
          if (pc.getWeapon() != null) {
            int lv2 = pc.getWeapon().getEnchantLevel();
            int gfx = 8684;
            switch (lv2) {
              case 0:
              case 1:
              case 2:
              case 3:
              case 4:
              case 5:
              case 6:
                gfx = 8684;
                break;
              case 7:
              case 8:
                gfx = 8685;
                break;
              case 9:
              case 10:
                gfx = 8773;
                break;
              default:
                gfx = 8686;
                break;
            } 
            pc.sendPacketsAll((ServerBasePacket)new S_SkillSound(pc.getId(), gfx));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1973));
          break;
        case 8:
          g_entertime = pc.getRocksPrisonTime();
          g_maxTime = MapsTable.get().getMapTime(56) * 60;
          g_newTime = (g_maxTime - g_entertime) / 60;
          if (g_newTime <= 0)
            g_newTime = 0; 
          rocksPrison = new S_ServerMessage(2535, "古鲁丁奇岩地监", String.valueOf(g_newTime));
          pc.sendPackets((ServerBasePacket)rocksPrison);
          i_entertime = pc.getIvoryTowerTime();
          i_maxTime = MapsTable.get().getMapTime(82) * 60;
          i_newTime = (i_maxTime - i_entertime) / 60;
          if (i_newTime <= 0)
            i_newTime = 0; 
          ivorytower = new S_ServerMessage(2535, "象牙塔", String.valueOf(i_newTime));
          pc.sendPackets((ServerBasePacket)ivorytower);
          l_entertime = pc.getLastabardTime();
          l_maxTime = MapsTable.get().getMapTime(479) * 60;
          l_newTime = (l_maxTime - l_entertime) / 60;
          if (l_newTime <= 0)
            l_newTime = 0; 
          lastabard = new S_ServerMessage(2535, "拉斯塔巴德广场", String.valueOf(l_newTime));
          pc.sendPackets((ServerBasePacket)lastabard);
          d_entertime = pc.getDragonValleyTime();
          d_maxTime = MapsTable.get().getMapTime(37) * 60;
          d_newTime = (d_maxTime - d_entertime) / 60;
          if (d_newTime <= 0)
            d_newTime = 0; 
          dragonvalley = new S_ServerMessage(2535, "龙之谷地监", String.valueOf(d_newTime));
          pc.sendPackets((ServerBasePacket)dragonvalley);
          break;
        case 9:
          pc.sendPackets((ServerBasePacket)new S_MapTimerOut(pc));
          break;
      } 
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
